package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//利用新的传输器，查询部门编号为1的数据
public class Test4 {
    public static void main(String[] args) throws Exception {
        //1，注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2，获取连接
        String url = "jdbc:mysql://localhost:3307/cgb211101?characterEncoding=utf8";
        Connection c = DriverManager.getConnection(url, "root", "root");
        //3,获取传输器，并执行sql骨架
        String sql = "select * from dept where deptno = ?";
        PreparedStatement s = c.prepareStatement(sql);
        //设置sql的参数--是指给第几个问号,设置什么值
        s.setObject(1,1);
        ResultSet r = s.executeQuery();
        //4,执行sql
        ;//执行查询的sql
        //5,解析结果集
        while (r.next()){
            Object deptno = r.getObject(1);//获取第1列的值
            Object dname = r.getObject(2);//获取第2列的值
            Object loc = r.getObject(3);//获取第3列的值
            System.out.println(""+deptno+dname+loc);

        }
        r.close();
        s.close();
        c.close();

    }
}
